package com.ai.bitcoin.schedule.task;

import com.ai.bitcoin.constant.Bcc;
import com.ai.bitcoin.jpa.entity.BccBasicInfo;
import com.ai.bitcoin.jpa.entity.BccPriceDay;
import com.ai.bitcoin.jpa.entity.BccPriceInfo;
import com.ai.bitcoin.jpa.service.BccBasicInfoService;
import com.ai.bitcoin.jpa.service.BccPriceDayService;
import com.ai.bitcoin.jpa.service.BccPriceInfoService;
import com.ai.bitcoin.redis.service.BccRedisService;
import com.ai.bitcoin.serialize.BeanSerial;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.List;

/**
 * @author Jason
 * @date 2018/4/8
 **/
@Component
public class StartupTask extends AbstractTask {

    @Autowired
    BccRedisService bccRedisService;
    @Autowired
    BccBasicInfoService bccBasicInfoService;
    @Autowired
    BccPriceDayService bccPriceDayService;
    @Autowired
    BccPriceInfoService bccPriceInfoService;

    @Override
    public void execute() {
        //priceDayNodeList
        List<BccBasicInfo> list = bccBasicInfoService.findByRecordState(Bcc.RecordState.PUBLISH);

        //今天的价格
        Calendar time = Calendar.getInstance();
        time.set(Calendar.HOUR, 0);
        time.set(Calendar.MINUTE, 0);
        time.set(Calendar.SECOND, 0);
        time.add(Calendar.DAY_OF_MONTH, -1);
        Sort sort = new Sort(Sort.Direction.ASC, "bccId");
        for (BccBasicInfo bccBasicInfo : list) {
            String bccId = bccBasicInfo.getBccId();
            String todayPrice = bccRedisService.getTodayPrice(bccId);
            if (StringUtils.isEmpty(todayPrice)) {
                Pageable pageable = PageRequest.of(0, pageSizeDb, sort);
                Page<BccPriceInfo> page = bccPriceInfoService.findByBccIdAndPriceTimestampGreaterThan(bccId, time.getTime(), pageable);
                List<BccPriceInfo> priceInfos = page.getContent();
                int pageNum = page.getTotalPages();
                for (int j = 0; j < pageNum; j++) {
                    if (j != 0) {
                        pageable = PageRequest.of(j, pageSizeDb, sort);
                        page = bccPriceInfoService.findByBccIdAndPriceTimestampGreaterThan(bccId, time.getTime(), pageable);
                        priceInfos = page.getContent();
                    }
                    bccRedisService.addTodayPriceNodeList(bccId, BeanSerial.list2JsonArray(priceInfos));
                }
            }
        }

        //每天5个点的价格
        for (BccBasicInfo bccBasicInfo : list) {
            String bccId = bccBasicInfo.getBccId();
            String dayPrice = bccRedisService.getDayPrice(bccId);
            if (StringUtils.isEmpty(dayPrice)) {
                Pageable pageable = PageRequest.of(0, pageSizeDb, sort);
                Page<BccPriceDay> page = bccPriceDayService.findByBccId(bccId, pageable);
                List<BccPriceDay> bccPriceDays = page.getContent();
                int pageNum = page.getTotalPages();
                for (int j = 0; j < pageNum; j++) {
                    if (j != 0) {
                        pageable = PageRequest.of(0, pageSizeDb, sort);
                        page = bccPriceDayService.findByBccId(bccId, pageable);
                        bccPriceDays = page.getContent();
                    }
                    bccRedisService.deleteDayPrice(bccId);
                    bccRedisService.addDayPriceNodeList(bccId, BeanSerial.list2JsonArray(bccPriceDays));
                }
            }
        }
    }
}
